package com.xindaoapp.happypet.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.FoundsAdapter;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.Founds;
import com.xindaoapp.happypet.ui.PostDetailActivity;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;

/**
 * 我参加的活动
 * 
 * @author Yangqing
 * 
 */
public class ActivityMyJoin extends BaseLoadAnimationAcitivity implements OnClickListener {
    private PullToRefreshListView listview;
    private ImageView tittle_common_cancel;
    private TextView tittle_my_jion;
    private int currentpage = 1;
    private TextView tittle_common_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_nearby);
        super.onCreate(savedInstanceState);
        listview = (PullToRefreshListView) findViewById(R.id.listview);
        tittle_common_cancel = (ImageView) findViewById(R.id.tittle_common_cancel);
        tittle_my_jion = (TextView) findViewById(R.id.tittle_my_jion);
        tittle_common_content = (TextView) findViewById(R.id.tittle_common_content);
        tittle_common_content.setText("我参加的");
        tittle_common_cancel.setOnClickListener(this);
        tittle_my_jion.setVisibility(View.GONE);
        tittle_my_jion.setOnClickListener(this);
        SharedPreferences sp = getSharedPreferences("userconfig", MODE_PRIVATE);
        adapter = new FoundsAdapter(this);
        initListView();
    }

    private void initListView() {
        listview = (PullToRefreshListView) findViewById(R.id.listview);
        listview.setMode(Mode.PULL_FROM_START);
        listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentpage = 0;
                getActivityInfo(currentpage, 0);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (total == adapter.getAll().size()) {
                    Toast.makeText(getApplicationContext(), "没有更多数据了", Toast.LENGTH_SHORT).show();
                    listview.onRefreshComplete();
                    return;
                } else {
                    currentpage++;
                    getActivityInfo(currentpage, 1);
                }
            }
        });
        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ActivityMyJoin.this, PostDetailActivity.class);
                intent.putExtra(Constants.KEY_POST_TID, adapter.getAll().get(position - 1).tid);
                startActivity(intent);
            }
        });
        getActivityInfo(currentpage, 0);
    }

    private int total;
    private FoundsAdapter adapter;
    private boolean isFirst = true;

    private void getActivityInfo(int page, final int loadtype) {
        RequestParams params = new RequestParams();
        params.put("page", String.valueOf(page));
        params.put("pagesize", "20");
        params.put("uid", UserState.getUserUid());
        AddClient.get(Address.MYJOIN, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                try {
                    isFirst = false;
                    onDataArrived(true);
                    Founds parse = JSON.parseObject(content, Founds.class);
                    if (parse.getResult().equals("0")) {
                        total = Integer.valueOf(parse.getTotal());
                        if (loadtype == 0) {
                            adapter.clearItems();
                            adapter.addItems(parse.array);
                            listview.getRefreshableView().setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        } else if (loadtype == 1) {
                            adapter.addItems(parse.array);
                            adapter.notifyDataSetChanged();
                        }
                    }
                    listview.onRefreshComplete();
                } catch (Exception e) {
                    listview.onRefreshComplete();
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Throwable error, String content) {
                listview.onRefreshComplete();
                if (isFirst) {
                    onDataArrived(false);
                    SetOnLoadFailListener(new OnLoadFailListener() {
                        @Override
                        public void onLoadFail() {
                            getActivityInfo(currentpage, 0);
                        }
                    });
                } else {
                    onDataArrived(true);
                }
                super.onFailure(error, content);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tittle_common_cancel:
                finish();
                break;
            default:
                break;
        }
    }

}
